import subprocess
import random


class ProxyManager:
    def __init__(self):
        # 初始化节点列表
        self.nodes = [
            {"host": "pro-iplc-hk1-1.dimsumyumcha.com", "port": 465, "password": "dyYQASSWL6jTIolKSK",
             "available": True}
        ]

    def get_available_node(self):
        # 获取一个可用的节点
        available_nodes = [node for node in self.nodes if node["available"]]
        if available_nodes:
            return random.choice(available_nodes)
        else:
            return None

    def create(self):
        # 创建 Shadowsocks 服务
        node = self.get_available_node()
        if node:
            # 将节点标记为不可用
            node["available"] = False
            # 创建 Shadowsocks 服务
            command = f'sslocal -s {node["host"]} -p {node["port"]} -k {node["password"]} -m aes-256-cfb -l 1080'
            subprocess.Popen(command, shell=True)
            print(f"Shadowsocks service created for {node['host']}:{node['port']}")
        else:
            print("No available Shadowsocks node.")

    def mark_node_available(self, host, port):
        # 将指定节点标记为可用
        for node in self.nodes:
            if node["host"] == host and node["port"] == port:
                node["available"] = True
                print(f"Marked node {host}:{port} as available.")
                break


# 使用示例
manager = ProxyManager()
manager.create()
